// | chiefComplaint        | 主诉     |
// eslint-disable-next-line max-len
import { chiefComplaint } from '@/utils/lib/EmrTextConvert/Adapter/chiefComplaint'
// | presentIllnessHistory | 现病史   |
// eslint-disable-next-line max-len
import { presentIllnessHistory } from '@/utils/lib/EmrTextConvert/Adapter/presentIllnessHistory'
// | pastHistory           | 既往史   |
import { pastHistory } from '@/utils/lib/EmrTextConvert/Adapter/pastHistory'
// | allergicHistory       | 过敏史   |

// | personalHistory       | 个人史   |
// eslint-disable-next-line max-len
import { personalHistory } from '@/utils/lib/EmrTextConvert/Adapter/personalHistory'

// | familyHistory         | 家族史   |
import { familyHistory } from '@/utils/lib/EmrTextConvert/Adapter/familyHistory'
// | bodyCheck             | 体格检查 |
import { bodyCheck } from '@/utils/lib/EmrTextConvert/Adapter/bodyCheck'
// | assExam               | 辅助检查 |
import {
  assExam,
  assExamImgUrls,
} from '@/utils/lib/EmrTextConvert/Adapter/assExam'

import { diagnosis } from '@/utils/lib/EmrTextConvert/Adapter/diagnosis'

// eslint-disable-next-line max-len
import { reviewChiefComplaint } from '@/utils/lib/EmrTextConvert/Adapter/reviewChiefComplaint'
// eslint-disable-next-line max-len
import { reviewDiagnosis } from '@/utils/lib/EmrTextConvert/Adapter/reviewDiagnosis'

import { cnDialectical } from '@/utils/lib/EmrTextConvert/Adapter/cnDialectical'
import { cnDiagnosis } from '@/utils/lib/EmrTextConvert/Adapter/cnDiagnosis'
import { replaceFirstAndLast, replaceRepeatSymbol } from './Utils/common'

import {
  lessThanControLevel,
  greaterEqualControLevel,
} from './Utils/controLevel'

import {
  lessThanAcuteControlLevels,
  greaterEqualAcuteControlLevels,
} from './Utils/acuteControlLevels'
export function firstEmrConver(data, ageType) {
  const emrConverText = []
  // 主诉
  const chiefComplaintContext = chiefComplaint(data)
  console.log('chiefComplaintContext =' + chiefComplaintContext)
  if (chiefComplaintContext)
    emrConverText.push({
      model: '主诉',
      text: replaceFirstAndLast(replaceRepeatSymbol(chiefComplaintContext)),
    })
  // 现病史
  const presentIllnessHistoryContext = presentIllnessHistory(data)
  console.log('presentIllnessHistoryContext =' + presentIllnessHistoryContext)
  if (presentIllnessHistoryContext)
    emrConverText.push({
      model: '现病史',
      text: replaceFirstAndLast(
        replaceRepeatSymbol(presentIllnessHistoryContext)
      ),
    })
  // 既往史
  const pastHistoryContext = pastHistory(data)
  console.log('pastHistoryContext =' + pastHistoryContext)
  if (pastHistoryContext)
    emrConverText.push({
      model: '既往史',
      text: replaceFirstAndLast(replaceRepeatSymbol(pastHistoryContext)),
    })
  // // 过敏史
  // const allergicHistoryContext = allergicHistory(data)
  // console.log('allergicHistoryContext =' + allergicHistoryContext)

  // 个人史
  const personalHistoryContext = personalHistory(data)
  console.log('personalHistoryContext =' + personalHistoryContext)
  if (personalHistoryContext)
    emrConverText.push({
      model: '个人史',
      text: replaceFirstAndLast(replaceRepeatSymbol(personalHistoryContext)),
    })
  // 家族史
  const familyHistoryContext = familyHistory(data)
  console.log('familyHistoryContext =' + familyHistoryContext)
  if (familyHistoryContext)
    emrConverText.push({
      model: '家族史',
      text: replaceFirstAndLast(replaceRepeatSymbol(familyHistoryContext)),
    })
  // // 体格检查
  const bodyCheckContext = bodyCheck(data)
  console.log('bodyCheckContext =' + bodyCheckContext)
  if (bodyCheckContext)
    emrConverText.push({
      model: '体格检查',
      text: replaceFirstAndLast(replaceRepeatSymbol(bodyCheckContext)),
    })
  // 辅助检查
  const assExamContext = assExam(data)
  const assExamContextImgUrls = assExamImgUrls(data)
  if (assExamContext)
    emrConverText.push({
      model: '辅助检查',
      text: replaceFirstAndLast(replaceRepeatSymbol(assExamContext)),
    })
  if (assExamContextImgUrls)
    emrConverText.push({
      model: '辅助检查图片',
      text: JSON.stringify(assExamContextImgUrls),
    })

  // 诊断
  const diagnosisContext = diagnosis(data, ageType)
  console.log('diagnosisContext =' + diagnosisContext)
  if (diagnosisContext)
    emrConverText.push({
      model: '诊断',
      text: replaceFirstAndLast(replaceRepeatSymbol(diagnosisContext)),
    })

  // 中医辩证
  const cnDialecticalContext = cnDialectical(data)
  console.log('cnDialecticalContext =' + cnDialecticalContext)
  if (cnDialecticalContext)
    emrConverText.push({
      model: '中医辨证',
      text: replaceFirstAndLast(replaceRepeatSymbol(cnDialecticalContext)),
    })

  // 中医诊断
  const cnDiagnosisContext = cnDiagnosis(data)
  console.log('cnDialecticalContext =' + cnDialecticalContext)
  if (cnDiagnosisContext)
    emrConverText.push({
      model: '中医诊断',
      text: replaceFirstAndLast(replaceRepeatSymbol(cnDiagnosisContext)),
    })

  return emrConverText
}

export function secondEmrConver(data, ageType) {
  const emrConverText = []
  // 主诉
  const reviewChiefComplaintContext = reviewChiefComplaint(data)
  console.log('chiefComplaintContext =' + reviewChiefComplaintContext)
  if (reviewChiefComplaintContext)
    emrConverText.push({
      model: '主诉',
      text: replaceFirstAndLast(
        replaceRepeatSymbol(reviewChiefComplaintContext)
      ),
    })

  // 体格检查
  const bodyCheckContext = bodyCheck(data)
  console.log('bodyCheckContext =' + bodyCheckContext)
  if (bodyCheckContext)
    emrConverText.push({
      model: '体格检查',
      text: replaceFirstAndLast(replaceRepeatSymbol(bodyCheckContext)),
    })

  // 辅助检查
  const assExamContext = assExam(data)
  console.log('assExamContext =' + assExamContext)
  if (assExamContext)
    emrConverText.push({
      model: '辅助检查',
      text: replaceFirstAndLast(replaceRepeatSymbol(assExamContext)),
    })

  // 诊断
  const reviewDiagnosisContext = reviewDiagnosis(data, ageType)
  console.log('reviewDiagnosisContext =' + reviewDiagnosisContext)
  if (reviewDiagnosisContext)
    emrConverText.push({
      model: '诊断',
      text: replaceFirstAndLast(replaceRepeatSymbol(reviewDiagnosisContext)),
    })

  // 中医辩证
  const cnDialecticalContext = cnDialectical(data)
  console.log('cnDialecticalContext =' + cnDialecticalContext)
  if (cnDialecticalContext)
    emrConverText.push({
      model: '中医辨证',
      text: replaceFirstAndLast(replaceRepeatSymbol(cnDialecticalContext)),
    })

  // 中医诊断
  const cnDiagnosisContext = cnDiagnosis(data)
  console.log('cnDialecticalContext =' + cnDialecticalContext)
  if (cnDiagnosisContext)
    emrConverText.push({
      model: '中医诊断',
      text: replaceFirstAndLast(replaceRepeatSymbol(cnDiagnosisContext)),
    })
  return emrConverText
}
/**
 * @param {*} attrs 传入病历的form
 * @param {*} treatmentType 首复诊
 * @retrun
 */
export function emrConverTextToAttrs(attrs, treatmentType, ageType) {
  const tmp = {}
  attrs.forEach((item) => {
    const { attrValue, name } = item
    tmp[name] = attrValue
  })
  if (treatmentType === 1) {
    return firstEmrConver(tmp, ageType)
  } else if (treatmentType === 0) {
    return secondEmrConver(tmp, ageType)
  }
  return []
}

/**
 * @param {*} attrs 传入病历的form
 * @param {*} treatmentType 首复诊
 * @retrun
 */
export function getEmrTextToKV(attrs, treatmentType, ageType) {
  const emrKVMap = {
    主诉: 'mainSuit',
    现病史: 'historyPresent',
    既往史: 'past',
    个人史: 'personal',
    家族史: 'family',
    体格检查: 'signs',
    辅助检查: 'auxiliaryExamination',
    辅助检查图片: 'imgUrl',
    诊断: 'diagnosis',
    中医辨证: 'cnDialectical',
    中医诊断: 'cnDiagnosis',
  }

  const convers = emrConverTextToAttrs(attrs, treatmentType, ageType)

  const kv = {}

  convers.forEach(({ model, text }) => {
    const key = emrKVMap[model]
    kv[key] = text
  })

  Object.keys(emrKVMap).forEach((key) => {
    // 先拿到接口需要用到的对应的属性字段
    const value = emrKVMap[key]
    // 先查是否已有 没有就补充空字段
    const text = kv[value]
    if (!text) {
      kv[value] = ''
    }
  })

  return kv
}

/**
 * 转换本地病历转换到控制水平
 * @param {*} attrs 传入病历的form
 * @param {*} age 填写病历时的年龄
 * @retrun 返回控制水平表格转换结果
 */
export function getLocalControLevel(attrs, age = -1) {
  if (age === -1) {
    throw new Error('需要传递年龄')
  }
  if (age > 6) {
    return greaterEqualControLevel(attrs, age)
  } else {
    return lessThanControLevel(attrs, age)
  }
}

/**
 * 转换本地病历转换到控制水平
 * @param {*} attrs 传入病历的form
 * @param {*} age 填写病历时的年龄
 * @retrun 返回控制水平表格转换结果
 */
export function acuteControlLevels(attrs, age = -1) {
  if (age === -1) {
    throw new Error('需要传递年龄')
  }
  if (age >= 6) {
    return greaterEqualAcuteControlLevels(attrs, age)
  } else {
    return lessThanAcuteControlLevels(attrs, age)
  }
}
